/*
 * @Author: zhouwr 1551541860@qq.com
 * @Date: 2024-02-17 20:22:25
 * @LastEditors: zhouwr 1551541860@qq.com
 * @LastEditTime: 2024-03-03 00:26:16
 * @FilePath: /jeecgboot-vue3/src/views/activiti/model/ActModel.api.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defHttp } from '/@/utils/http/axios'
import { useMessage } from '/@/hooks/web/useMessage'

const { createConfirm } = useMessage()

enum Api {
  listPage = '/act/model/listPage',
  listHistoryPage = '/act/model/listHistoryPage',
  save = '/act/model/add',
  copy = '/act/model/copy',
  edit = '/act/model/edit',
  deploy = '/act/model/deploy',
  deleteOne = '/act/model/delete',
  deleteBatch = '/act/model/deleteBatch',
  importExcel = '/act/model/importExcel',
  exportXls = '/act/model/exportXls',
  exportXml = '/act/model/exportXml',
  getModelXml = '/act/model/getModelXml',
}

/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls
/**
 * 导入api
 */
export const getImportUrl = Api.importExcel
/**
 * 列表接口
 * @param params
 */
export const listPage = (params) => defHttp.get({ url: Api.listPage, params })

export const listHistoryPage = (params) => defHttp.get({ url: Api.listHistoryPage, params })

/**
 * 删除单个
 */
export const deleteOne = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteOne, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess()
  })
}
/**
 * 批量删除
 * @param params
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}

/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdate = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save
  return defHttp.post({ url: url, params })
}

/**
 * 复制流程
 * @param params
 */
export const copy = (params) => defHttp.post({ url: Api.copy, params })

/**
 * 流程部署
 * @param params
 */
export const deploy = (params) => {
  return defHttp.get({ url: Api.deploy, params })
}

/**
 * 流程xml导出
 * @param params
 */
export const exportXml = (params) => {
  const ele = document.createElement('a')
  ele.setAttribute('href', Api.exportXml + '?id=' + params.id) //设置下载文件的url地址
  ele.setAttribute('download', 'model.xml') //用于设置下载文件的文件名
  ele.click()
}

/**
 * 流程xml获取
 * @param params
 * @param handleSuccess
 */
export const getModelXml = (params, handleSuccess, handleError) => {
  return defHttp.get({ url: Api.getModelXml, params }, { isTransformResponse: false })
    .then((rsp) => {
      console.log('getModelXml', rsp);
      if (rsp && rsp.success) {
        handleSuccess(rsp.result)
      } else {
        handleError(rsp.message)
      }
    })
    .catch((err) => {
      console.error(err)
    })
}
